package com.olclass.web.action;


import com.olclass.web.domain.User;
import com.olclass.web.service.UserService;
import com.olclass.web.util.BaseObjectResponse;
import com.olclass.web.util.BaseResponse;
import com.olclass.web.util.CommonConstants;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Controller
@RequestMapping("/login")
public class LoginAction {

    private Logger logger = Logger.getLogger(LoginAction.class);

    @Autowired
    UserService loginService;

    /**
     * 登录验证接口
     * @param request
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/validate.action", method = {RequestMethod.GET, RequestMethod.POST},
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse validate(HttpServletRequest request,
                                    @RequestParam(value = "username") String username,
                                    @RequestParam(value = "password") String password) {

        BaseObjectResponse response = new BaseObjectResponse();

        try {
            String token = "";
            User user =  loginService.checkLogin(username, password);
            if(user!=null){
                token = DigestUtils.md5Hex(user.getUsername()+user.getPassword()+user.getPhone());
                user.setToken(token);
            }else{
                throw new Exception("未找到用户");
            }
            HttpSession session = request.getSession();
            session.setAttribute("access_token",token);
            session.setAttribute("username",user.getUsername());
            session.setAttribute("userid",user.getId());
            response.setData(user);
        } catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
            response.setMessage(ex.getMessage());
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
        }

        return response;
    }


    @RequestMapping(value = "/logout.action", method = {RequestMethod.GET, RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse logout(HttpServletRequest request,
                               @RequestParam(value = "token") String token) {

        BaseResponse response = new BaseResponse();

        try {
            HttpSession session = request.getSession();
            request.getSession().invalidate();
            session.removeAttribute("access_token");
            session.removeAttribute("username");
            session.removeAttribute("userid");
        } catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
            response.setMessage(ex.getMessage());

        }

        return response;
    }

}
